1461D - Divide and Summarize - CodeForces Solution


binary search brute force data structures divide and conquer implementation sortings *1600

Please click on ads to support us..

C++ Code:

//                                    MAY LORD BLESS YO PITTY ARSE SOUL
/*

    ███▄ ▄███▓▓█████  ██▓  ██████ ▄▄▄█████▓▓█████  ██▀███                 ▓█████▄ ▓█████  ███▄ ▄███▓
    ▓██▒▀█▀ ██▒▓█   ▀ ▓██▒▒██    ▒ ▓  ██▒ ▓▒▓█   ▀ ▓██ ▒ ██▒               ▒██▀ ██▌▓█   ▀ ▓██▒▀█▀ ██▒
    ▓██    ▓██░▒███   ▒██▒░ ▓██▄   ▒ ▓██░ ▒░▒███   ▓██ ░▄█ ▒               ░██   █▌▒███   ▓██    ▓██░
    ▒██    ▒██ ▒▓█  ▄ ░██░  ▒   ██▒░ ▓██▓ ░ ▒▓█  ▄ ▒██▀▀█▄                 ░▓█▄   ▌▒▓█  ▄ ▒██    ▒██
    ▒██▒   ░██▒░▒████▒░██░▒██████▒▒  ▒██▒ ░ ░▒████▒░██▓ ▒██▒ ██▓  ██▓  ██▓ ░▒████▓ ░▒████▒▒██▒   ░██▒
    ░ ▒░   ░  ░░░ ▒░ ░░▓  ▒ ▒▓▒ ▒ ░  ▒ ░░   ░░ ▒░ ░░ ▒▓ ░▒▓░ ▒▓▒  ▒▓▒  ▒▓▒  ▒▒▓  ▒ ░░ ▒░ ░░ ▒░   ░  ░
    ░  ░      ░ ░ ░  ░ ▒ ░░ ░▒  ░ ░    ░     ░ ░  ░  ░▒ ░ ▒░ ░▒   ░▒   ░▒   ░ ▒  ▒  ░ ░  ░░  ░      ░
    ░      ░      ░    ▒ ░░  ░  ░    ░         ░     ░░   ░  ░    ░    ░    ░ ░  ░    ░   ░      ░
        ░      ░  ░ ░        ░              ░  ░   ░       ░    ░    ░     ░       ░  ░       ░




        ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
        ⠀⠀⠀⠀⣠⣶⡾⠏⠉⠙⠳⢦⡀⠀⠀       ⠀⢠⠞⠉⠙⠲⡀⠀
        ⠀⠀⠀⣴⠿⠏⠀⠀⠀⠀⠀⠀⢳⡀⠀        ⡏⠀⠀⠀⠀⠀⢷
        ⠀⠀⢠⣟⣋⡀⢀⣀⣀⡀⠀⣀⡀⣧⠀       ⢸⠀⠀⠀⠀⠀ ⡇
        ⠀⠀⢸⣯⡭⠁⠸⣛⣟⠆⡴⣻⡲⣿⠀    .. ⣸⠀HELLO⠀⡇
        ⠀⠀⣟⣿⡭⠀⠀⠀⠀⠀⢱⠀⠀⣿⠀  .    ⢹⠀⠀⠀⠀⠀ ⡇
        ⠀⠀⠙⢿⣯⠄⠀⠀⠀⢀⡀⠀⠀⡿⠀.       ⡇⠀⠀⠀⠀⡼
        ⠀⠀⠀⠀⠹⣶⠆⠀⠀⠀⠀⠀⡴⠃⠀       ⠀⠘⠤⣄⣠⠞⠀
        ⠀⠀⠀⠀⠀⢸⣷⡦⢤⡤⢤⣞⣁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
        ⠀⠀⢀⣤⣴⣿⣏⠁⠀⠀⠸⣏⢯⣷⣖⣦⡀⠀⠀⠀⠀⠀⠀
        ⢀⣾⣽⣿⣿⣿⣿⠛⢲⣶⣾⢉⡷⣿⣿⠵⣿⠀⠀⠀⠀⠀⠀
        ⣼⣿⠍⠉⣿⡭⠉⠙⢺⣇⣼⡏⠀⠀⠀⣄⢸⠀⠀⠀⠀⠀⠀
        ⣿⣿⣧⣀⣿………⣀⣰⣏⣘⣆⣀⠀⠀
*/


#include <bits/stdc++.h>
#include <algorithm>
#pragma GCC optimize("O2")
using namespace std;

#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<long long>
#define vvl vector<vector<ll>>
#define yes "YES";
#define no "NO";
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repe(i, a, b) for (ll i = a; i <= b; i++)
#define repi(i, a, b, k) for (ll i = a; i < b; i += k)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define input(a, n)  \
    rep(i, 0, n)     \
    {                \
        cin >> a[i]; \
    }
ll MOD = 1000000007;
// llindexL,indexR,total2;
struct DSU
{
    vector<ll> parent, sizz;
    DSU() {}
    DSU(ll n)
    {
        for (ll i = 0; i <= n; i++)
            parent.push_back(i);
        sizz.assign(n + 1, 1);
    }
    ll find_set(ll v)
    {
        if (parent[v] == v)
            return v;
        return parent[v] = find_set(parent[v]);
    }
    void union_set(ll u, ll v)
    {
        u = find_set(u);
        v = find_set(v);

        if (v == u)
            return;

        if (sizz[u] > sizz[v])
            swap(u, v);
        parent[u] = v;
        sizz[v] += sizz[u];
    }
};

void dfs()
{
}

vector<bool> prime(1e6 + 1, true);
void SieveOfEratosthenes()
{
    for (int p = 2; p * p <= 1e6; p++)
    {
        if (prime[p] == true)
        {
            for (int i = p * p; i <= 1e6; i += p)
                prime[i] = false;
        }
    }
}
void solve(vll &a,vll &pref,set<ll> &mp,ll l,ll r)
{
    mp.insert(pref[r]-pref[l-1]);
    if(a[l-1]==a[r-1])
        return;

    ll mid=(a[l-1]+a[r-1])/2;
    ll idx=upper_bound(a.begin(),a.end(),mid)-a.begin();
    solve(a,pref,mp,l,idx);
    solve(a,pref,mp,idx+1,r);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(9);
    cin.tie(nullptr);
    cout.tie(nullptr);

    ll tc;
    tc = 1;
    cin >> tc;
    while (tc--)
    {
        ll n,q;
        cin>>n>>q;

        vll a(n);
        input(a,n);
        sort(a.begin(),a.end());

        vll pref(n+1);
        rep(i,0,n)
            pref[i+1]=pref[i]+a[i];
        
        set<ll> mp;
        solve(a,pref,mp,1,n);

        rep(i,0,q)
        {
            ll s;
            cin>>s;

            if(mp.find(s)!=mp.end()) cout<<"YES\n";
            else cout<<"NO\n";
        }

    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram